import grace from "../../utils/grace.js"
grace.page({
      data: {
            isChoose: 0,
            address: []
      },
      onLoad: function(options) {
            this.loadData();
            if (options.length > 0) {
                  let params = options[0];
                  if (params) {
                        this.$data.isChoose = params[0].isChoose;
                  }
            }
            this.$bus.$on("update_addres_event", (data) => {
                  this.loadData();
            })
      },
      loadData: function() {
            this.$http.get('api/user_address/list', {}).then((res) => {
                  this.$data.address = res.list;
            });

      },
      openAddressUpdate: function(e) {
            let address_id = e.currentTarget.dataset.address;
            wx.navigateTo({
                  url: '../address_add/address_add?address_id=' + address_id
            })
      },
      setDefault: function(e) {
            if (this.$data.isChoose) {
                  let address_id = e.currentTarget.dataset.address;
                  this.$http.post('api/user_address/set_defaul', {
                        address_id: address_id
                  }).then((res) => {
                        this.$bus.$emit("update_addres_event");
                        this.$goBack();
                  });
            }
      },
      $onBackData(data) {
            this.log('onBackData', data);
            this.loadData();
      }
})